/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package library.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import library.InventoryChangeListener;
import library.Item;
import library.Library;
import library.User;

/**
 *
 * @author ryan
 */
public class UserPanel extends JPanel{
    JComboBox userDropDown;
    private Library library;
    JScrollPane userItemsPane;
    List<ItemPane> currentUserItems;
    DefaultListModel m;
    JList userItems = null;
    
    public UserPanel(Library library){
        this.library = library;        
        
        currentUserItems = new ArrayList<ItemPane>();
        
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        JPanel topUserPanel = new JPanel();
        
        topUserPanel.setLayout(new BoxLayout(topUserPanel, BoxLayout.X_AXIS));
        userDropDown = new JComboBox();
        
        topUserPanel.setMaximumSize(new Dimension(300, 20));
        topUserPanel.add(userDropDown);
        add(topUserPanel);
        
        JButton addUser = new JButton("Add Patron");
        addUser.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                AddUserPopup addUser = new AddUserPopup(UserPanel.this);
                addUser.setVisible(true);
            }        
        });
        
        topUserPanel.add(addUser);
        /*
        JCheckBox overdue = new JCheckBox("Overdue Items");
        add(overdue);
        */
        
        m = new DefaultListModel();
        userItems =  new JList(m);       
        userItems.setCellRenderer(new MyListCellRenderer());        
        JScrollPane spInventory = new JScrollPane(userItems);
        add(spInventory);
        
        userDropDown.addItemListener(new ItemListener(){

            @Override
            public void itemStateChanged(ItemEvent e) {
                setUserItems((User)e.getItem());          
            }
        });
        
        this.library.addDateChangeListeners(new InventoryChangeListener(){

            @Override
            public void onChange() {
                System.out.println("Date change fired");
                for(int i=0;i<m.getSize();i++){
                    
                    Object o = m.get(i);
                    m.set(i, o);
                }
            }
        });
        
        for(User u: library.getUsers()){
            addUser(u);
        }        
    }
    
       
    private void setUserItems(User u){
        m.clear();
        for(Item i:u.getItems()){
            m.addElement(i);
        }
    }
   
    public Item getSelectedItem(){
        return (Item)userItems.getSelectedValue();
    }
    
    public User getSelectedUser(){
        return (User)userDropDown.getSelectedItem();
    }
    
    public final void addUser(final User u){
        u.addInventoryChangeListener(new InventoryChangeListener(){
            @Override
            public void onChange() {
                setUserItems(u);
            }        
        });
        library.addUser(u);
        userDropDown.addItem(u);
        userDropDown.setSelectedItem(u);
    } 
    
    class MyListCellRenderer implements ListCellRenderer{ 
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            ItemPane item = new ItemPane((Item)value);
            if(index%2==0){
                item.setBackground(Color.WHITE);
            }
            if(cellHasFocus){
                item.setBackground(Color.BLUE);
            }if(item.getItem().getDueDate().before(library.getDate())){
                item.setBackground(Color.PINK);
            }
            return item;       
        }
    }
}
